import java.io.*;
import java.util.*;
import java.sql.*;
import java.text.*;

public class GetUser
{
	public static void main(String [] args)
	{
		String site_cd = args[0];
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		PrintStream log = null;
		int MAX = 0;
		
		try
		{
			System.out.println(site_cd+" user to File....");
		
			// DB connect 
			Class.forName("oracle.jdbc.driver.OracleDriver");
			
			//Dev DB
			//conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.18:1521:ORARU","GSMS","imsi00");
			//Live DB
			conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.14:1521:ORARU","GSMS","imsi00");	
			stmt = conn.createStatement();
			
			String sql =  "select a.id, a.email			"
					+ "from                                 "
					+ "    (                                "
					+ "     SELECT user_no, id , email      "
					+ "     FROM g_mb_member                "
					+ "     WHERE ( email IS NOT NULL       "
					+ "           AND email <> 'WAP_TEMP'   "
					+ "          )                          "
					+ "      AND reg_country = '"+site_cd+"'"
					+ "      AND deleted IS NULL            "
					+ "      AND u_email_flag = 0           "
					+ "    ) a,                             "
					+ "    (                                "
					+ "    SELECT user_no                   " 
					+ "    FROM g_mb_optin                  "
					+ "     where code = '0113001'          "
					+ "    ) b                              "
					+ "where a.user_no = b.user_no          ";
		
			rs = stmt.executeQuery(sql);
			
			int j = 1;
			int i = 1;

			if( ( log = new PrintStream(new FileOutputStream("./users_"+site_cd+j+".txt")) ) == null ) 
				throw new Exception("File Created Error!!");
			
			while(rs.next()) {
				//save data from DB			
				log.println("ID" + i + " : " + rs.getString("ID"));
				log.println("EMAIL" + i + " : " + rs.getString("EMAIL"));
				i++;

				if(i == 150000) {

                        		log.println("ID" + i + " : free_"+site_cd);
                        		log.println("EMAIL" + i + " : jinwoo.wang@partner.samsung.com");
                        		i++;
                        		log.println("ID" + i + " : free_"+site_cd);
                        		log.println("EMAIL" + i + " : OKullig@hotmail.com");
					log.close();	

		 			if( ( log = new PrintStream(new FileOutputStream("./users_"+site_cd+(++j)+".txt")) ) == null ) 
						throw new Exception("File Created Error!!");

					i = 1;	
				}
			}

			log.println("ID" + i + " : free_"+site_cd);
			log.println("EMAIL" + i + " : jinwoo.wang@partner.samsung.com");
			i++;
			log.println("ID" + i + " : free_"+site_cd);
			log.println("EMAIL" + i + " : OKullig@hotmail.com");
			
		 	log.close();
			stmt.close();
			conn.close();
		
		}catch(Exception ee) {
		  	System.out.println("Exception!! : [" + ee.getMessage() + "]");
		}finally {
		  	try { if(log != null) { log.close(); log = null; } } catch(Exception loge) {}
		  	try { if(stmt != null) { stmt.close(); stmt = null; } } catch(Exception stmte) {}
		  	try { if(conn != null) { conn.close(); conn = null; } } catch(Exception cone) {}
		}
	}
}
